<?php
header("Content-Type: application/vnd.ms-excel");
header('Content-Disposition: attachment; filename="AccountSale.xls"'); 
$reqPath='';
//require_once("include/authen.php");
require_once($reqPath."class/m_report.php");
	$datefrom = $_POST['dFrom'];
	$dateto = $_POST['dTo'];
	
	list($day, $month, $year) = split('/', $datefrom);
	$c_from =$year."/".$month."/".$day;

	list($day, $month, $year) = split('/', $dateto);
	$c_to =$year."/".$month."/".$day;
 
?>
<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
 
<HEAD>
<meta http-equiv="Content-type" content="text/html;charset=utf8" />
</HEAD>
<BODY>

<span>
<?php 
echo "รายงานยอดขาย  ". $datefrom . " ถึง " . $dateto;
?>
</span>
<TABLE  x:str BORDER="1">

<?php
 
 $report=new M_report();
 $data=$report->AccountSale($c_from, $c_to);
$totalProduct =0;
$totalTreatment=0;
$totalSet=0; 
$totalSmart=0;
$totalCredit=0;
$totalPayCash=0;
$totalPayCredit=0;
$totalPaySmartPurse=0;
$totalCreditRemain=0;
$totalPdDiscount=0;
$totaTmDiscount=0;
$totalSetDiscount=0; 
$totalCourseCut=0;

echo "<tr>";
echo "<td rowspan='2' align='center'> วันที่ </td>";
echo "<td rowspan='2' align='center'> ยอดรวม </td>";
echo "<td colspan='4' align='center'> ยอดขาย  </td>";
echo "<td rowspan='2' align='center'> เติมเงิน </td>";
echo "<td rowspan='2' align='center'> รับชำระลูกหนี้ </td>";
echo "<td rowspan='2' align='center'> ใช้บริการคอร์สเดิม</td>";
echo "<td rowspan='2' align='center'> ยอดรวม </td>";
echo "<td colspan='3' align='center'> ชำระโดย </td>";
echo "<td rowspan='2' align='center'> ลูกหนี้ค้างชำระ </td>";
echo "<td colspan='5' align='center'> ส่วนลด </td>";
echo "<td rowspan='2' align='center'> ตัดชำระคอร์สเดิม </td>";
echo "</tr>";

echo "<tr>";
echo "<td align='center'> ผลิตภัณฑ์  </td>";
echo "<td align='center'> ทริทเมนท์  </td>";
echo "<td align='center'> เซ็ท/คอร์ส  </td>";
echo "<td align='center'> บัตรสมาชิก  </td>";

echo "<td align='center'> เงินสด  </td>";
echo "<td align='center'> บัตรเครดิต  </td>";
echo "<td align='center'> สมาร์ท  </td>";

echo "<td align='center'> ผลิตภัณฑ์  </td>";
echo "<td align='center'> ทริทเมนท์  </td>";
echo "<td align='center'> เซ็ท/คอร์ส  </td>";
echo "<td align='center'> บัตรสมาชิก  </td>";
echo "<td align='center'> คูปอง  </td>";
 
echo "</tr>";	
       
while($row = $data->FetchRow()){
	$totalA=$row['Product'] + $row['Treatment'] +  $row['Set'] +  $row['SmartPurse'] + $row['Credit'];
	$totalB=$row['PayCash'] + $row['PayCredit'] + $row['PaySmartPurse'];
	echo "<tr>";
	echo "<td>".$row['Date']."</td>";
	echo "<td>".$totalA."</td>"; // Total
	echo "<td>".$row['Product']."</td>"; //Product
	echo "<td>".$row['Treatment']."</td>"; //Treament
	echo "<td>".$row['Set']."</td>"; //Set/course
	echo "<td>0</td>";  // member
	echo "<td>".$row['SmartPurse']."</td>"; //smartPurse ref
	echo "<td>".$row['Credit']."</td>"; //CreditSale
	echo "<td>0</td>"; //course
	echo "<td>".$totalB."</td>"; // Total
	echo "<td>".$row['PayCash']."</td>"; //PayCash
	echo "<td>".$row['PayCredit']."</td>"; //PayCredit
	echo "<td>".$row['PaySmartPurse']."</td>"; //PaySmartPurse
	echo "<td>".$row['CreditRemain']."</td>"; //CreditRemain
	echo "<td>".$row['ProductDiscount']."</td>"; //ProductDiscount
	echo "<td>".$row['TreatmentDiscount']."</td>"; //TreatmentDiscount
	echo "<td>".$row['SetDiscount']."</td>"; //SetDiscount
	echo "<td>0</td>"; //member
	echo "<td>0</td>"; //Coupon
	echo "<td>".number_format($row['CourseCut'],2,'.',',') ."</td>"; //CourseCut
 	echo "</tr>";	

	$totalProduct +=$row['Product'];
	$totalTreatment+=$row['Treatment'];
	$totalSet+=$row['Set']; 
	$totalSmart+=$row['SmartPurse'];
	$totalCredit+=$row['Credit'];
	$totalPayCash+=$row['PayCash'];
	$totalPayCredit+=$row['PayCredit'];
	$totalPaySmartPurse+=$row['PaySmartPurse'];
	$totalCreditRemain+=$row['CreditRemain'];
	$totalPdDiscount+=$row['ProductDiscount'];
	$totaTmDiscount+=$row['TreatmentDiscount'];
	$totalSetDiscount+=$row['SetDiscount']; 
	$totalCourseCut+=$row['CourseCut'] ;
	
}
$totalC=$totalProduct + $totalTreatment +  $totalSet +  $totalSmart + $totalCredit;
$totalD=$totalPayCash + $totalPayCredit + $totalPaySmartPurse ;
echo "<tr>";
echo "<td><b>ยอดรวม</b></td>";  
echo "<td>".$totalC."</td>"; // Total
echo "<td>".$totalProduct."</td>"; //Product
echo "<td>".$totalTreatment."</td>"; //Treament
echo "<td>".$totalSet."</td>"; //Set/course
echo "<td>0</td>";  // member
echo "<td>".$totalSmart."</td>"; //smartPurse ref
echo "<td>".$totalCredit."</td>"; //CreditSale
echo "<td>0</td>"; //course
echo "<td>".$totalD."</td>"; // Total
echo "<td>".$totalPayCash."</td>"; //PayCash
echo "<td>".$totalPayCredit."</td>"; //PayCredit
echo "<td>".$totalPaySmartPurse."</td>"; //PaySmartPurse
echo "<td>".$totalCreditRemain."</td>"; //CreditRemain
echo "<td>".$totalPdDiscount."</td>"; //ProductDiscount
echo "<td>".$totaTmDiscount."</td>"; //TreatmentDiscount
echo "<td>".$totalSetDiscount."</td>"; //SetDiscount
echo "<td>0</td>"; //member
echo "<td>0</td>"; //Coupon
echo "<td>". number_format($totalCourseCut,2,'.',',')  ."</td>"; //CourseCut
echo "</tr>";

?>
</table>
</BODY>
</html>